# 通过jsonpath来筛选数据
import requests
from jsonpath import jsonpath
from fake_useragent import FakeUserAgent

# 定义请求头
headers = {'User-Agent': FakeUserAgent().random}

# 定义爬取页数
pages = input('请输入想要爬取的页数：')

# 通过for循环来爬取多页数据
for i in range(int(pages)):
    # 定义请求网址
    url = f'https://movie.douban.com/j/chart/top_list?type=24&interval_id=100%3A90&action=&start={i * 20}&limit=20'

    # 发起网络请求
    response = requests.get(url, headers=headers)

    # 转化为json数据并进行筛选
    json_data = response.json()
    ranks = jsonpath(json_data, '$..rank')
    titles = jsonpath(json_data, '$..title')
    types = jsonpath(json_data, '$..types')

    # 通过for循环来将筛选内容输出
    for rank, title, type in zip(ranks, titles, types):
        print('排名:' + str(rank))
        print('电影名:' + title)
        print(f'电影类型:{type}')
        print('==========================================')
